<?php
class mis_pdes extends Model 
{
    public static function getTableName(){
        return 'mis_pdes';
    }

    /**
     ** check if exists mis record in mis_pdes mapping table
     **
     ** @param $type   : type
     ** @param $mis_id : mis id
     **
     ** @return boolean
     **/
    public static function exists($type, $mis_id)
    {
        $sql = 'SELECT mis_id FROM mis_pdes where type = :type and mis_id = :misid ';
        $ret = Model::selectBySql($sql,array(':type'=>$type,':misid'=>$mis_id));
        return ($ret ? $ret[0]['mis_id']  : false);
    }

    /*
    public static function batchReplace($new_data)
    {
        $ids = array();
        if(empty($new_data))
        {
            return false; 
        }
        foreach($new_data as $table => $obj)
        {
            if($obj)
            {
                $update_id = Model::$dbo['w']->save($obj);
                $ids[$table] = $update_id;  
            }
          
        }
        if (!empty($ids))
        {
            return $ids;
        }
        else
        {
            return false;
        }

    }
     */
    public static function replace($new_data)
    {
        if(empty($new_data))
        {
            return false; 
        }
        $update_id = Model::$dbo['w']->save($new_data);
               
       
        if (!empty($update_id))
        {
            return $update_id;
        }
        else
        {
            return false;
        }

    }


    public static function getRelation($array)
    {
        if(!empty($array))
        {
            $sql_head = 'SELECT * FROM mis_pdes where ';
            $sql_sub = '';
            foreach($array as $key => $val)
            {
                $sql_sub = $sql_sub.$key .'= "'.$val.'" and ';
            }
            $sql_foot = 'pdes_id != 0';
            $sql = $sql_head.$sql_sub.$sql_foot;
            $ret = Model::selectBySql($sql,array());
            return ($ret ? $ret  : false);
        }
        else
        {
            return false;
        }
    }

    public static function updateRelation($relation)
    {
        $mis_pdes = new mis_pdes();
        if(empty($relation))
        {
            return FALSE;
        }

        $mis_pdes->co_code = isset($relation['co_code']) ? $relation['co_code'] : '';
        $mis_pdes->type     = $relation['type'];
        $mis_pdes->mis_id   = (int)$relation['mis_id'];
        $mis_pdes->mis_eid  = (int)$relation['mis_eid'];
        $mis_pdes->mis_pid  = (int)$relation['mis_pid'];
        $mis_pdes->pdes_id  = (int)$relation['pdes_id'];
        $mis_pdes->pdes_eid = (int)$relation['pdes_eid'];
        $mis_pdes->pdes_pid = (int)$relation['pdes_pid'];
        $ret = Model::$dbo['w']->save_by_fields($mis_pdes,array('type','mis_id')); 
        return empty($ret) ? false : $ret ;
    }
 
}
?>
